from django.db import models
from django.contrib.auth import get_user_model
# Create your models here.
User = get_user_model()

# 博客分类表
class BlogCategory(models.Model):
    name = models.CharField(max_length=100, verbose_name='分类名称')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '博客分类'
        verbose_name_plural = verbose_name
# 博客详情表
class BlogDetail(models.Model):
    title = models.CharField(max_length=100, verbose_name='标题')
    content = models.TextField(verbose_name='内容')
    category = models.ForeignKey(BlogCategory, on_delete=models.CASCADE, verbose_name='分类')
    pub_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='作者')

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = '博客详情'
        verbose_name_plural = verbose_name
        ordering = ['-pub_time']


# 博客评论表
class BlogComment(models.Model):
    content = models.TextField(verbose_name='评论内容')
    pub_time = models.DateTimeField(auto_now_add=True, verbose_name='评论时间')
    author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='评论者')
    blog = models.ForeignKey(BlogDetail, on_delete=models.CASCADE,related_name='comments', verbose_name='所属博客')

    def __str__(self):
        return self.content

    class Meta:
        verbose_name = '博客评论'
        verbose_name_plural = verbose_name
        ordering = ['-pub_time']